package logic.pruebas;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

import logic.characters.Player;

/**
 * @author Unai
 * Panel donde se llevan a cabo las partidas
 *
 */
public class GamePanel extends JPanel {
	private static final long serialVersionUID = -7607918187368085879L;
	private int delay = 1000;
	public int time;
	public Timer timer;
	private JLabel timeLabel;
	private JProgressBar life;
	private JProgressBar stamina;
	private Player player;


	/**
	 * @param time
	 */
	public GamePanel(int time) {
		this.time = time;
		timeLabel = new JLabel();
		add(timeLabel, 0);
		timeLabel.setBounds(new Rectangle(100, 20));
		timeLabel.setLocation(650,10);
		timeLabel.setForeground(Color.WHITE);
		timeLabel.setFont(new Font("Consolas", Font.BOLD, 20));
		timeLabel.setText("Time: " + time);
		
		ActionListener taskPerformer = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				reduceTime();
			}
		};

		timer = new Timer(delay, taskPerformer);
	}
	
	/**
	 * Reduce el tiempo del temporizador
	 */
	public void reduceTime() {
		time -= 1;
		timeLabel.setText("Time: " + time);
	}
	
	/**
	 * Dibuja el tiempo
	 */
	public void drawTime() {
		timeLabel.repaint();
		add(timeLabel, 0);
	}
	
	/**
	 * @param player
	 * Inicializa el jugador
	 */
	public void setPlayer(Player player) {
		this.player = player;
		initBars();
	}
	
	/**
	 * Inicializa la barra de jugador y de stamina del jugador
	 */
	public void initBars() {
		life = new JProgressBar(0, player.maxLife);
		stamina = new JProgressBar(0, player.maxStamina);
		life.setStringPainted(false);
		stamina.setStringPainted(false);
		life.setBounds(new Rectangle(150, 15));
		stamina.setBounds(new Rectangle(150, 15));
		life.setLocation(60, 10);
		life.setForeground(Color.RED);
		stamina.setLocation(60, 30);
		stamina.setForeground(Color.GREEN);
		
	}
	
	/**
	 * Actualiza las barras
	 */
	public void updateBars() {
		life.setValue(player.life);
		stamina.setValue(player.stamina);
		add(life, 0);
		add(stamina, 0);
	}
}